//
//  RadioDetailViewController.m
//  A Cup
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 doll. All rights reserved.
//

#import "RadioDetailViewController.h"
#import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"
#import "Radio.h"
#import "dataBaseHandle.h"
#import "RadioList.h"
#import "RadioListCell.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "RadioDetailInfoViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface RadioDetailViewController ()
@property(nonatomic,retain)NSMutableArray *listArr;
@property(nonatomic,retain)NSDictionary *radioInfo;
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)UIImageView *imageCoverIMG;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,retain)UIImageView *userIMG;
@property(nonatomic,retain)UILabel *desc;
@property(nonatomic,retain)UILabel *musicVisitNum;
@property(nonatomic,retain)NSNumber *total;
@property(nonatomic,assign)CGRect rect;
@end

@implementation RadioDetailViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.listArr = [NSMutableArray array];
        self.radioInfo = [NSDictionary dictionary];
    }
    [self listData];
    return self;
}

- (void)dealloc
{
    [_userinfo release];
    [_listArr release];
    [_radioInfo release];
    [_tableView release];
    [_imageCoverIMG release];
    [_userIMG release];
    [_desc release];
    [_musicVisitNum release];
    [_total release];
    [super dealloc];
}

- (void)listData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.radioid,@"radioid",@"1",@"client",@"D8FDE94C-841F-4A4D-B5C4-AAD2472CEE85",@"deviceid",@"",@"auth",@"3.0.1",@"version", nil];
    [manager POST:@"http://api2.pianke.me/ting/radio_detail" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dataDic = [responseObject objectForKey:@"data"];
        NSArray *tempArr = [dataDic objectForKey:@"list"];
        self.total = [dataDic objectForKey:@"total"];
        self.radioInfo = [dataDic objectForKey:@"radioInfo"];

        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
        [self.view addSubview:self.tableView];
        [_tableView release];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Helvetica-Bold" size:18],NSFontAttributeName, nil];
        self.desc.text = self.radioInfo[@"desc"];
        CGRect rect = [self.desc.text boundingRectWithSize:CGSizeMake(WIDTH - 60, 400) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

        self.desc.frame = CGRectMake(15, 250, WIDTH - 60, rect.size.height);
        self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 270 + rect.size.height)];

        NSString *musicVisitNum = [NSString stringWithFormat:@"%@",self.radioInfo[@"musicvisitnum"]];

        UILabel *uName = [[UILabel alloc]initWithFrame:CGRectMake(55, 205, 200, 30)];
        uName.text = self.userinfo[@"uname"];
        [self.tableView.tableHeaderView addSubview:uName];
        [uName release];
        self.desc.numberOfLines = 0;
        [self.desc sizeToFit];
        [self.tableView.tableHeaderView addSubview:self.desc];
        [_desc release];

        self.userIMG = [[UIImageView alloc]initWithFrame:CGRectMake(10, 200,40, 40)];
        NSURL *URLImg = [NSURL URLWithString:self.userinfo[@"icon"]];
        [self.userIMG sd_setImageWithURL:URLImg];
        self.userIMG.layer.cornerRadius = 20;
        self.userIMG.clipsToBounds = YES;
         [self.tableView.tableHeaderView addSubview:self.userIMG];
        [_userIMG release];

        UIImageView *musicVisitIMG = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - 110, 205, 20, 20)];
        musicVisitIMG.image = [UIImage imageNamed:@"musicVisitNum"];
        [self.tableView.tableHeaderView addSubview:musicVisitIMG];
        [musicVisitIMG release];

        self.musicVisitNum = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH - 90, 205, 75, 20)];
        self.musicVisitNum.font = [UIFont systemFontOfSize:15];
        self.musicVisitNum.text = musicVisitNum;
        [self.tableView.tableHeaderView addSubview:self.musicVisitNum];
        [_musicVisitNum release];

        UIImageView *imageCoverIMG = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 180)];

        self.imageCoverIMG = imageCoverIMG;
        NSString *str = [self.radioInfo objectForKey:@"coverimg"];

        NSURL *radioURL = [NSURL URLWithString:str];

        [self.imageCoverIMG sd_setImageWithURL:radioURL];
         [self.tableView.tableHeaderView addSubview:imageCoverIMG];
        [imageCoverIMG release];

        for (NSDictionary *tempDic in tempArr) {
            RadioList *radioList = [[RadioList alloc]init];
            [radioList setValuesForKeysWithDictionary:tempDic];
            [self.listArr addObject:radioList];
            [radioList release];
        }
        [self method];
        [self.tableView reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
    self.page++;
}

- (void)listLoadData
{
    if ([dataBaseHandle shareDB].state == 1) {
        NSString *i = [NSString stringWithFormat:@"%ld",self.page * 10];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"D8FDE94C-841F-4A4D-B5C4-AAD2472CEE85",@"deviceid",self.radioid,@"radioid",@"1",@"client",@"10",@"limit",@"3.0.1",@"version",i,@"start",@"",@"auth", nil];
        [manager POST:@"http://api2.pianke.me/ting/radio_detail_list" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *dataDic = [responseObject objectForKey:@"data"];
            NSArray *dataArr = [dataDic objectForKey:@"list"];
            for (NSDictionary *tempDic in dataArr) {
                RadioList *radioList = [[RadioList alloc]init];
                [radioList setValuesForKeysWithDictionary:tempDic];
                [self.listArr addObject:radioList];
                [radioList release];
            }
            NSInteger totallist = [self.total intValue];
            if (self.page == (totallist/10 + 1)) {
                [self.tableView.footer noticeNoMoreData];
            }
            [self.tableView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }
         ];
        self.page ++;
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前无网络，请检查网络后重新尝试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
}

- (void)method
{
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        [self listLoadData];
        [self.tableView.footer endRefreshing];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self listData];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.tableView reloadData];
    self.desc = [[UILabel alloc]init];
    self.desc.font =[UIFont fontWithName:@"Helvetica-Bold" size:18];

    self.tabBarController.tabBar.hidden = NO;
    self.page = 1;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(turnBack)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:59/255.0 green:54/255.0 blue:24/255.0 alpha:1];
}

- (void)turnBack
{
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    RadioListCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[RadioListCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.title.text = [self.listArr[indexPath.row] title];
    NSURL *coverimg = [NSURL URLWithString:[self.listArr[indexPath.row]coverimg]];
    [cell.myImageView sd_setImageWithURL:coverimg];
    cell.musicVisitImageView.image = [UIImage imageNamed:@"musicVisitNum"];
    cell.musicVisit.text = [self.listArr[indexPath.row] musicVisit];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RadioList *radioList = self.listArr[indexPath.row];
    RadioDetailInfoViewController *RadioDetailInfoVC = [[RadioDetailInfoViewController alloc]init];
    [self.navigationController pushViewController:RadioDetailInfoVC animated:YES];
    RadioDetailInfoVC.tingid = [radioList tingid];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
